#ifndef __FUNCTION_H__
#define __FUNCTION_H__

/**
 * 上机实验用到的函数
 */

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cmath>
#include "Polynomial.h"

#define PI acos(-1)

using namespace std;

double Bfunc(double x);
double Cfunc(double x);
vector<vector<double> >& BgenerateVector(const int N);
vector<vector<double> >& CgenerateVector(const int N);
vector<vector<bool> >& generateBoolVector(const int N, const int Num = 1);
vector<double>& Chebyshev(const int N);


double Bfunc(double x){
     return 1.0/(1+x*x);
}

double Cfunc(double x){
     return 1.0/(1+25*x*x);
}

/**
 * 生成B题插值序列
 */
vector<vector<double> >& BgenerateVector(const int N){
  vector<vector<double> >* result = new vector<vector<double> >;
  vector<double> vecx(N+1);
  vector<double> vecvalue(N+1);
  for (int i = 0 ; i <= N ; i++ ){
    vecx[i] = -5+10.0*i/N;
    vecvalue[i] = Bfunc(vecx[i]);
  }
  result->push_back(vecx);
  result->push_back(vecvalue);
  return *result;
}

/**
 * 生成C题插值序列
 */
vector<vector<double> >& CgenerateVector(const int N){
  vector<vector<double> >* result = new vector<vector<double> >;
  vector<double> vecx = Chebyshev(N);
  vector<double> vecvalue(N);
  for (int i = 0 ; i < N ; i++ ){
    vecvalue[i] = Cfunc(vecx[i]);
  }
  result->push_back(vecx);
  result->push_back(vecvalue);
  return *result;
}


vector<vector<bool> >& generateBoolVector(const int N, const int Num){
  vector<vector<bool> >* result = new vector<vector<bool> >;
  vector<bool> vecbool[Num+1];
  for (int i = 0 ; i <= Num ; i++){
    vecbool[i].resize(N+1,1);
    result->push_back(vecbool[i]);
  }
  return *result;
}

// 切比雪夫节点
vector<double>& Chebyshev(const int N){
     vector<double>* result = new vector<double>;
     for (int i = 1 ; i <= N ; i++){
          result->push_back(cos((2*i-1)*PI/(2*N)));
     }
     return *result;
}


#else
//do nothing
#endif
